//
//  JiJiangJieXiaoController.swift
//  HappyEat
//
//  Created by ctx2016 on 17/1/6.
//  Copyright © 2017年 陌路是我. All rights reserved.
//

import UIKit

class JiJiangJieXiaoController: UIViewController ,UITableViewDelegate,UITableViewDataSource{
    let JIJINGCELLID:String = "JiJIangjiexiaoCellId"
    var tableView:UITableView = UITableView.init()
    //倒计时类
    var countDown:CountDown?
    
    var dataSources:[String]?
    var timer:Timer?
    override func viewDidLoad() {
        super.viewDidLoad()
        self.setNavi()
        self.createTableView()
        self.loadData()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        UIApplication.shared.setStatusBarStyle(.lightContent, animated: true)
    }
}

extension JiJiangJieXiaoController {
    //MARK:设置导航栏选项
    func setNavi(){
       self.navigationController?.navigationBar.barTintColor = .red
        self.navigationController?.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.white]
    }
    //MARK:CreateTableView
    func createTableView(){
       self.view.addSubview(self.tableView)
        self.tableView.snp.makeConstraints { (make) in
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.top.equalTo(0)
            make.bottom.equalTo(0)
        }
        self.tableView.delegate = self
        self.tableView.dataSource = self
        tableView.register(JiJiangJieXiaoCell.self , forCellReuseIdentifier: JIJINGCELLID)
        tableView.separatorStyle = .none
    }
    //MARK:loadData
    func loadData(){
     self.dataSources = ["2017-1-20 15:45:02","2017-1-21 14:24:10","2017-1-21 14:24:07","2017-1-17 14:25:01","2017-1-23 14:24:11","2017-1-21 14:34:08","2017-1-22 14:26:03","2017-1-23 22:23:49","2017-1-24 14:23:43"]
    self.countDown = CountDown.init()
    weak var WeakSelf = self
        self.countDown?.countDown(per_SECBlock: { 
            WeakSelf?.updateTimeInVisibleCells()
        })
    }
    //MARK:更新cell中的倒计时
    func updateTimeInVisibleCells(){
        //取出屏幕可见的Cell
        let cellArrays = self.tableView.visibleCells
        for cell in cellArrays{
            (cell as!JiJiangJieXiaoCell).timeLabel.text = self.getNowTimeWithString((dataSources?[cell.tag])!)
        }
    }
    //MARK:getNowTimeWithString
    
    func getNowTimeWithString(_ atimeStrig:String)->String{
        let formate:DateFormatter =  DateFormatter.init()
        formate.dateFormat = "yyyy-MM-dd HH:mm:ss"
        //截止时间date格式
        let expireDate:Date = formate.date(from: atimeStrig)!
        var nowDate:Date = Date.init()
        //当前时间字符串格式
        let nowDateStr:String = formate.string(from: nowDate)
        //当前时间格式
        nowDate = formate.date(from: nowDateStr)!
        let timeInterVal:Double = expireDate.timeIntervalSinceNow
        
        let days:Int = Int(timeInterVal/(3600*24))
        let hours = Int(timeInterVal/3600)
        let minus = Int(((timeInterVal -  Double(hours*3600))/60))
        let seconds = Int(timeInterVal - Double(hours * 3600) - Double(minus * 60))
        
        var dayStr:String = String(days)
        var hourStr:String = String(hours)
        var minusStr:String = String(minus)
        var secondStr:String = String(seconds)
        if hours < 10{
            hourStr = "0"+hourStr
        }
        if minus < 10 {
            minusStr = "0" + minusStr
        }
        if seconds < 10 {
            secondStr = "0" + secondStr
        }
        if (hours <= 0)&&(minus <= 0)&&(seconds <= 0){
            return "00:00:00"
        }
        return hourStr+":"+minusStr+":"+secondStr
    }
    //MARK:TableViewDelegate-Delegate
    @objc(numberOfSectionsInTableView:) func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    @objc(tableView:didSelectRowAtIndexPath:) func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
//        print("cell isSelected")
        let vc:JieXiaoDetailController = JieXiaoDetailController()
        self.navigationController?.pushViewController(vc, animated: true)
        
    }
    @objc(tableView:cellForRowAtIndexPath:) func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell:JiJiangJieXiaoCell = tableView.dequeueReusableCell(withIdentifier: JIJINGCELLID, for: indexPath) as! JiJiangJieXiaoCell
//        let cell = JiJiangJieXiaoCell.init(style: .default, reuseIdentifier: JIJINGCELLID)
        cell.addLabel.text = "合肥商之都"
        cell.nameLabel.text = "芝士蛋糕"
        cell.timeLabel.text = self.getNowTimeWithString(self.dataSources![indexPath.row])
        cell.tag = indexPath.row
        cell.selectionStyle  = .none
        return cell
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        guard  let dataArray:[String] = self.dataSources else {
            return 0
        }
        return dataArray.count
    }
    @objc(tableView:heightForRowAtIndexPath:) func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 75 + 26
    }
}
